package io.afu.solusvm.dto;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author RffanLAB.方露宇
 * @version 0.1
 */
public class SolusVMResp {

    private String status;

    private String statusmsg;

    private String vmstat;

    private String hostname;

    private String ipaddress;

    private String ipaddr;

    private String hdd;

    private String mem;

    private String bw;


    /**
     * 获取所有的IP
     * @return ip的数组
     */
    private List<String> pickUpIps(){
        List<String> ips = new ArrayList<>();
        if (this.ipaddr != null){
            ips = Arrays.asList(this.ipaddr.split(","));
        }
        return ips;
    }

    /**
     * 是否正确执行
     * @return 返回是或者否
     */
    public boolean actionExcuted(){
        if (this.status != null){
            if (this.status.equals("success")){
                return true;
            }
        }
        return false;
    }

    /**
     * 是否在线
     * @return 返回是或者否
     */
    public boolean isOnline(){
        if (this.vmstat != null && !this.vmstat.equals("")){
            if (this.vmstat.equals("online")){
                return true;
            }
        }
        if (this.statusmsg != null && !this.statusmsg.equals("")){
            if (this.statusmsg.equals("online")){
                return true;
            }
        }
        return false;
    }


    public static SolusVMResp parse(String result){
        SolusVMResp solusVMResp = new SolusVMResp();
        solusVMResp.status = getSubUtil(result,"<status>(.*?)</status>");
        solusVMResp.statusmsg = getSubUtil(result,"<statusmsg>(.*?)</statusmsg>");
        solusVMResp.hostname = getSubUtil(result,"<hostname>(.*?)</hostname>");
        solusVMResp.ipaddress = getSubUtil(result,"<ipaddress>(.*?)</ipaddress>");
        solusVMResp.vmstat = getSubUtil(result,"<vmstat>(.*?)</vmstat>");
        solusVMResp.ipaddr = getSubUtil(result,"<ipaddr>(.*?)</ipaddr>");
        solusVMResp.hdd = getSubUtil(result,"<hdd>(.*?)</hdd>");
        solusVMResp.bw = getSubUtil(result,"<bw>(.*?)</bw>");
        solusVMResp.mem = getSubUtil(result,"<mem>(.*?)</mem>");
        return solusVMResp;
    }


    public static String getSubUtil(String soap,String rgex){
        Pattern pattern = Pattern.compile(rgex);// 匹配的模式
        Matcher m = pattern.matcher(soap);
        while (m.find()) {
            return m.group(1);
        }
        return null;
    }










    public String getIpaddr() {
        return ipaddr;
    }

    public void setIpaddr(String ipaddr) {
        this.ipaddr = ipaddr;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getStatusmsg() {
        return statusmsg;
    }

    public void setStatusmsg(String statusmsg) {
        this.statusmsg = statusmsg;
    }

    public String getVmstat() {
        return vmstat;
    }

    public void setVmstat(String vmstat) {
        this.vmstat = vmstat;
    }

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    public String getIpaddress() {
        return ipaddress;
    }

    public void setIpaddress(String ipaddress) {
        this.ipaddress = ipaddress;
    }

    public String getHdd() {
        return hdd;
    }

    public void setHdd(String hdd) {
        this.hdd = hdd;
    }

    public String getMem() {
        return mem;
    }

    public void setMem(String mem) {
        this.mem = mem;
    }

    public String getBw() {
        return bw;
    }

    public void setBw(String bw) {
        this.bw = bw;
    }
}
